<template>
  <div style="display: flex; margin-bottom: 20px">
    <el-input
      style="width: 300px"
      placeholder="输入文章标题"
      v-model="params.searchTitle"
      clearable
    ></el-input>
    <el-button type="primary" style="margin-left: 10px" @click="getList"
      >搜索</el-button
    >
  </div>
  <el-table :data="userData" style="width: 100%">
    <el-table-column prop="id" label="ID" />
    <el-table-column prop="title" label="标题" />
    <el-table-column prop="status" label="状态">
      <template #default="{ row }">
        <el-tag type="info" v-if="row.status == Enums.ARTICLE_ENUMS.待审核"
          >待审核</el-tag
        >
        <el-tag type="success" v-if="row.status == Enums.ARTICLE_ENUMS.已发布"
          >已发布</el-tag
        >
        <el-tag
          type="danger"
          v-if="row.status == Enums.ARTICLE_ENUMS.审核不通过"
          >审核不通过</el-tag
        >
      </template>
    </el-table-column>
    <el-table-column prop="createAt" label="创建时间" />
    <el-table-column prop="action" label="操作">
      <template #default="{ row }">
        <el-button @click="look(row)" size="small">查看</el-button>
        <el-button
          @click="Submit(true, row.id)"
          type="success"
          size="small"
          v-if="row.status == Enums.ARTICLE_ENUMS.待审核"
          >通过</el-button
        >
        <el-button
          @click="Submit(false, row.id)"
          type="danger"
          size="small"
          v-if="row.status == Enums.ARTICLE_ENUMS.待审核"
          >不通过</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <el-pagination
    style="margin-top: 20px"
    background
    layout="total, prev, pager, next"
    :total="total"
    :page-size="params.pageSize"
    @current-change="change"
  ></el-pagination>
  <el-dialog title="文章详情" v-model="detailDialog">
    <div style="max-height: 70vh; min-height: 50vh; overflow-y: auto">
      <p>{{ currentDetail }}</p>
    </div>
  </el-dialog>
</template>
<script setup>
import { ref, watch } from "vue";
import Enums from "@/utils/Enums";
import { getArticleList } from "@/api/article";
import { auditArticle } from "@/api/admin";
import { ElMessage, ElMessageBox } from "element-plus";

const userData = ref([]);
const detailDialog = ref(false);
const currentDetail = ref("");
const params = ref({
  page: 1,
  pageSize: 10,
  searchTitle: "",
});
const total = ref(0);
const getList = async () => {
  getArticleList(params.value)
    .then((res) => {
      userData.value = res.data.data.records;
      total.value = res.data.data.total;
    })
    .catch((err) => {
      console.error(err);
    });
};
const change = (page) => {
  params.value.page = page;
  getList();
};
const look = (row) => {
  currentDetail.value = row.content;
  detailDialog.value = true;
};
const Submit = async (status, id) => {
  if (status) {
    const res = await auditArticle({
      approved: status,
      articleId: id,
    });
    if (res.data.success) {
      ElMessage.success("操作成功");
      getList();
    } else {
      ElMessage.error(res.msg);
    }
    return;
  }
  ElMessageBox.confirm(`是否${status ? "通过" : "不通过"}该文章？`, "提示", {
    type: "warning",
    showCancelButton: true,
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    inputPlaceholder: "请输入理由",
    showInput: true,
    inputValue: "",
  })
    .then(async ({ value }) => {
      const res = await auditArticle({
        approved: status,
        articleId: id,
        comment: value,
      });
      if (res.data.success) {
        ElMessage.success("操作成功");
        getList();
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch(() => {});
};
getList();
</script>
